//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:2020/9/11
// @Time:1:00 下午
//
//

#ifndef GEEJOAN_NETWORKMANAGER_H
#define GEEJOAN_NETWORKMANAGER_H

#include "boost/asio/io_context.hpp"
#include "boost/asio/ip/tcp.hpp"
#include "Connection.h"
#include "BaseComponent.h"
#include <mutex>

namespace GeeJoan
{
/**
 * @brief 服务端网络封装 管理所有客户端 的链接
 */
    class NetworkManager : public BaseComponent, public BaseNetwork
    {
        std::mutex m_mutex;

        friend class Connection;

        boost::asio::io_context io_context;
        boost::asio::ip::tcp::acceptor acceptor_;
        int clientid = 0;
        std::map<int, std::shared_ptr<Connection>> m_client_map;

        void do_accept();

        int remove_client(int id);


    public:

        void close_client(int id);

        void init() override;

        void init_complete(Global_Context *context) override;

        int getOnlineCount() const;

        void start();

        NetworkManager(boost::asio::io_context &io_context, unsigned short port);

        virtual ~NetworkManager();

        int sendMsg(std::unique_ptr<HanderDataType> header, StatusCodeTypeEnum code, const std::string &msg ="", std::function<void(int status)> callback = nullptr);
    };

}
#endif //GEEJOAN_NETWORKMANAGER_H
